package com.wanxi.servlet.product;

import com.alibaba.fastjson2.JSONObject;
import com.wanxi.dto.ResultDTO;
import com.wanxi.model.ProductModel;
import com.wanxi.model.ResultModel;
import com.wanxi.service.ProductService;
import com.wanxi.service.impl.ProductServiceImpl;
import com.wanxi.util.Tool;
import com.wanxi.vo.ProductVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/back/product/add")
public class ProductAddServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //        1.乱码处理
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
//        2.得到前端的指
        String name = req.getParameter("name");
        String normalPrice = req.getParameter("normalPrice");
        String marketPrice = req.getParameter("marketPrice");
        String content = req.getParameter("content");
        String isShow = req.getParameter("isShow");
        String isRecommend = req.getParameter("isRecommend");
        String imgHref = req.getParameter("imgHref");
        String whereShow = req.getParameter("whereShow");
        String cuisineId = req.getParameter("cuisineId");
        String enable = req.getParameter("enable");

//        3.封装model
        ProductVO model = ProductVO.builder()
                .enable(Tool.strToInt(enable))
                .name(name)
                .imgHref(imgHref)
                .normalPrice(Tool.strToDecimal(normalPrice))
                .marketPrice(Tool.strToDecimal(marketPrice))
                .content(content)
                .isRecommend(Tool.strToInt(isRecommend))
                .isShow(Tool.strToInt(isShow))
                .cuisineId(Tool.strToInt(cuisineId))
                .build();
//        4.调用逻辑层处理逻辑
        ProductService productService = new ProductServiceImpl();
        ResultDTO resultModel = productService.add(model);
//        5.将数据转发给前端：io流的方式
        resp.getWriter().println(JSONObject.toJSONString(resultModel));

    }
}
